<?php
$pageTitle = "培训费用列表";
require_once 'header.php';

// 获取ID
if (!isset($_GET['id'])) {
    header('Location: index.php');
    exit;
}
// 获取培训ID
$training_id = intval($_GET['id']);

// 获取该培训的所有费用记录
$stmt = $pdo->prepare("SELECT * FROM trainings_cost WHERE trainings_id = ?");
$stmt->execute([$training_id]);
$cost_records = $stmt->fetchAll();

// 计算总费用
$total_cost = array_sum(array_column($cost_records, 'cost'));
$total_other_cost = array_sum(array_column($cost_records, 'other_cost'));
$total_all = $total_cost + $total_other_cost;
?>

<div class="container mt-5">
    <h2 class="mb-4">培训费用列表</h2>
    
    <?php if (empty($cost_records)): ?>
        <div class="alert alert-info">暂无费用填报记录。</div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table table-striped table-bordered">
                <thead class="table-dark">
                    <tr>
                        <th>学校</th>
                        <th>费用</th>
                        <th>其他费用</th>
                        <th>填报时间</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($cost_records as $record): ?>
                        <tr>
                            <td><?= htmlspecialchars($record['school']) ?></td>
                            <td><?= number_format($record['cost'], 2) ?></td>
                            <td><?= number_format($record['other_cost'], 2) ?></td>
                            <td><?= $record['created_at'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot class="table-secondary">
                    <tr>
                        <td><strong>总计</strong></td>
                        <td><strong><?= number_format($total_cost, 2) ?></strong></td>
                        <td><strong><?= number_format($total_other_cost, 2) ?></strong></td>
                        <td><strong><?= number_format($total_all, 2) ?></strong></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    <?php endif; ?>
</div>

<?php require_once 'footer.php'; ?>